package com.hsnn.medstgmini.base.dict.dao.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.DicEntprodProfileLogDao;
import com.hsnn.medstgmini.dao.impl.HibernateSqlUtilImpl;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 企业产品资质 类型日志
 * @author 王炳
 * @date 2015年7月13日
 */
@Repository
public class DicEntprodProfileLogDaoImpl implements DicEntprodProfileLogDao {

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	
	@Autowired
	private HibernateSqlUtilImpl dao;
	
	/**
	 * @category 获取企业产品资质 类型日志列表
	 * @date 2015年7月13日
	 * @return page
	 */
	@Override
	public Pagination getDicEntprodProfileLogList(Pagination page){
		Map<String,String> params = new HashMap<String, String>();
		String dicEntprodProfileId = page.getConditions().get("dicEntprodProfileId");
		String startDate = page.getConditions().get("startDate");
		String endDate = page.getConditions().get("endDate");
		StringBuffer sql = new StringBuffer();
		sql.append(" select a.first_level_indi_name firstLevelIndiName,a.first_level_indi_code firstLevelIndiCode,  a.log_id logId, ")
		   .append(" a.status status,a.detail detail,DATE_FORMAT(a.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,b.name updUser ")
		   .append(" from dic_entprod_profile_log a ")
		   .append(" left join sys_user b on b.id=a.upd_user ")
		   .append(" where  dic_entprod_profile_id =:dicEntprodProfileId ");
		params.put("dicEntprodProfileId", dicEntprodProfileId);
		if(StringUtils.isNotBlank(startDate)){
			sql.append(" and a.upd_datetime >= :startDate  ");
			params.put("startDate", startDate+" 00:00:00");
		}
		if(StringUtils.isNotBlank(endDate)){
			sql.append(" and a.upd_datetime <= :endDate  ");
			params.put("endDate", endDate+" 23:59:59");
		}
		sql.append(" order by a.upd_datetime desc");
		return dao.getSqlPageMapList(sql.toString(), params, page);
	}
	
	/**
	 * @category 获取企业产品资质 类型日志详情
	 * @date 2015年7月13日
	 * @return page
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,Object>> getDicEntprodProfileLogDetail(String id){
		Map<String,String> params = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer();
		sql.append("select t.first_level_indi_code firstLevelIndiCode, t.first_level_indi_name firstLevelIndiName,t.remark remark,t.status status,DATE_FORMAT(t.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,p.name updUser  ")
		   .append(" from dic_entprod_profile_log t left join sys_user p on p.id = t.upd_user ")
		   .append("  where t.dic_entprod_profile_id = (select b.dic_entprod_profile_id from dic_entprod_profile_log b where b.log_id = :id)  ")
		   .append(" and t.upd_datetime <= (select a.upd_datetime from dic_entprod_profile_log a where a.log_id = :id) ")
		   .append(" order by t.upd_datetime desc limit 2 ");
		params.put("id", id);
		return (List<Map<String, Object>>) dao.getSqlMapList(sql.toString(), params);
	}
}	
